﻿var YzWebUploader = (function (module) {
    module.uploaderImg = null;//上传图片控件（多图片）
    module.uploaderFile = null;//上传文档控件
    module.uploaderVideo = null;//上传视频控件 
    module.uploaderSingleImg = null;//单图片上传控件
    module.uploaderExcel = null;//导入Excel
    module.uploaderCustom = null;// null;//自定义上传控件

    module.hasExcelFile = false;//是否已经把excel文件选到队列
    module.queuedFileCount = 0;//队列中的文件数（多选上传）
    module.maxFileCount = 9;//最大可上传文件数
    module.uploadedCount = 0;//已经存在的文件数（常用于修改再次上传）
    module.guid = WebUploader.guid().replace("wu_", "");
    ///下面是方法
    ///统一绑定图片上传，文件上传，视频上传(successEvent  上传成功回调事件)
    module.BindUploader = function (successEvent) {
        module.BindUploaderImg(successEvent);
        module.BindUploaderFile(successEvent);
        module.BindUploaderVideo(successEvent);
    }
    ///上传图片
    module.BindUploaderImg = function (successEvent) {
        document.getElementById("btnUploaderImg").value = null;
        var ft = 1;
        module.uploaderImg = WebUploader.create({
            auto: true,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: '../File/UploadFile',
            pick: {
                id: '#btnUploaderImg'
            },
            formData: {
                "ft": ft
            },
            resize: false,
            accept: {                     // 指定接受哪些类型的文件
                title: 'Images',
                extensions: 'jpg,jpeg,png,gif,bmp',
                mimeTypes: 'image/jpg,image/jpeg,image/png,image/gif,image/bmp'
            },
            fileSingleSizeLimit: 10 * 1024 * 1024,       //验证单个文件大小是否超出限制
            duplicate: true               //去重
        });
        module.BindUploaderEvent(module.uploaderImg, ft, successEvent);
    }
    ///上传文件
    module.BindUploaderFile = function (successEvent) {
        var ft = 0;
        module.uploaderFile = WebUploader.create({
            auto: true,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: '../File/UploadFile',
            pick: '#btnUploaderFile',
            formData: {
                "ft": ft
            },
            resize: false,
            accept: {                     // 指定接受哪些类型的文件
                title: '文档',
                extensions: 'pdf,docx,doc,xls,xlsx,ppt,pptx',
                mimeTypes: '.pdf,.docx,.doc,.xls,.xlsx,.ppt,.pptx'
            },
            fileSingleSizeLimit: 20 * 1024 * 1024,       //验证单个文件大小是否超出限制
            duplicate: true               //去重
        });
        module.BindUploaderEvent(module.uploaderFile, ft, successEvent);
    }
    ///上传视频
    module.BindUploaderVideo = function (successEvent) {
        var ft = 2;
        module.uploaderVideo = WebUploader.create({
            auto: true,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: '../File/UploadBigFile',
            pick: {
                id: '#btnUploaderVideo', multiple: false
            },
            resize: false,
            formData: {
                "guid": module.guid
            },
            accept: {                     // 指定接受哪些类型的文件
                title: 'Video',
                extensions: 'mp4',
                mimeTypes: 'video/mp4'
            },
            fileSingleSizeLimit: 200 * 1024 * 1024,       //验证单个文件大小是否超出限制
            duplicate: true,               //去重
            chunked: true,
            chunkSize: 20 * 1024 * 1024,
            prepareNextFile: true
        });
        module.BindUploaderEvent(module.uploaderVideo, ft, successEvent);
        module.uploaderVideo.on('uploadSuccess', function (file, response) {
            if (response.res == true) {//上传成功！
                if (response.chunk == true) {
                    $.post("../File/MergeUploadFile", {
                        "d": new Date(), "fileName": file.name, "guid": module.guid
                    }, function (result) {
                        module.webUploaderSuccess(result, ft, successEvent);
                    })
                }
                else {
                    module.webUploaderSuccess(response, ft, successEvent);
                }
            }
            else {
                $.popup(response.msg);
            }
        });
    }
    ///上传单一图片（上传头像，海报等）
    module.BindUploaderSingleImg = function (successEvent, serverUrl, Id) {
        var serUrl = "../File/UploadFile";
        var pickId = "btnUploaderSingleImg";
        var ft = 1;
        if (Id != undefined && Id != "") {
            pickId = Id;
        }
        if (serverUrl != undefined && serverUrl != "") {
            serUrl = serverUrl;
        }
        module.uploaderSingleImg = WebUploader.create({
            auto: true,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: serUrl,
            pick: {
                id: "#" + pickId, multiple: false
            },
            formData: {
                "ft": ft
            },
            resize: false,
            accept: {
                title: 'Images',
                extensions: 'jpg,jpeg,png,gif,bmp',
                mimeTypes: 'image/jpg,image/jpeg,image/png,image/gif,image/bmp'
            },
            fileSingleSizeLimit: 10 * 1024 * 1024,       //验证单个文件大小是否超出限制
            multiple: false,
            duplicate: true               //去重
        });
        module.BindUploaderEvent(module.uploaderSingleImg, ft, successEvent);
    }
    ///导入Excel文档
    module.BindUploaderExcel = function (Id, successEvent, queuedEvent, serverUrl, para, isAuto, errEvent) {
        var ft = -1;
        if (isAuto == false) {
            para = {
            };
        }
        module.uploaderExcel = WebUploader.create({
            auto: isAuto,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: serverUrl,
            pick: '#' + Id,
            formData: para,
            resize: false,
            accept: {                     // 指定接受哪些类型的文件
                title: 'Excel文档',
                extensions: 'xls,xlsx',
                mimeTypes: 'application/msexcel'
            },
            fileSingleSizeLimit: 10 * 1024 * 1024,       //验证单个文件大小是否超出限制
            duplicate: true               //去重
        });
        module.BindUploaderEvent(module.uploaderExcel, ft, successEvent, queuedEvent, errEvent);
    }
    //自定义上传（单个文件）根据classname绑定上传控件
    module.BindUploaderCustom = function (successEvent, pickId, accept, isAuto, serverUrl) {
        if (pickId == undefined || pickId == "" || accept == undefined || accept.title == undefined || accept.title == "" || accept.extensions == undefined || accept.extensions == "" || accept.mimeTypes == undefined || accept.mimeTypes == "") {
            return false;
        }
        var serUrl = "../File/UploadFile";
        if (serverUrl != undefined && serverUrl != "") {
            serUrl = serverUrl;
        }
        var auto = true;
        if (isAuto != undefined) {
            auto = isAuto;
        }
        var ft = 3;
        module.uploaderCustom = WebUploader.create({
            auto: auto,
            swf: '../Scripts/webuploader/Uploader.swf',
            server: serUrl,
            pick: {
                id: "#" + pickId, multiple: false
            },
            formData: {
                "ft": ft
            },
            resize: false,
            accept: accept,//{
            //    title: 'Images',
            //    extensions: 'jpg,jpeg,png,gif,bmp',
            //    mimeTypes: 'image/jpg,image/jpeg,image/png,image/gif,image/bmp'
            //},
            fileSingleSizeLimit: 10 * 1024 * 1024,       //验证单个文件大小是否超出限制
            multiple: false,
            duplicate: true               //去重
        });
        module.BindUploaderEvent(module.uploaderCustom, ft, successEvent);
    }
    /*  以下为公用方法  */
    ///上传事件封装
    module.BindUploaderEvent = function (obj, ft, successEvent, queuedEvent, errEvent) {
        obj.on('uploadProgress', module.webUploaderProgress);
        if (ft != 2) {
            obj.on('uploadSuccess', function (file, response) {
                if (obj == module.uploaderExcel) {
                    module.hasExcelFile = false;
                }
                module.webUploaderSuccess(response, ft, successEvent, obj, errEvent);
            });
        }
        obj.on('error', function (e) {
            module.uploaderObjError(e, ft, errEvent);
        });
        if (obj == module.uploaderExcel) {
            obj.on("fileQueued", function (file) {
                module.hasExcelFile = true;
                if (queuedEvent != undefined && queuedEvent != null) {
                    queuedEvent(file);
                }
            });
        }
        else {
            obj.on("fileQueued", module.webUploaderFileQueued);
        }
        obj.on('uploadError', function (file) {
            module.webUploaderError(file, errEvent);
        });
        obj.on('uploadComplete', module.webUploaderComplete);
        obj.on('beforeFileQueued', module.webUploaderBeforeFileQueued);

    }
    ///文件添加到队列
    module.webUploaderFileQueued = function (file) {
        module.queuedFileCount++;
    }
    ///文件添加到队列之前的判断
    module.webUploaderBeforeFileQueued = function (file) {
        var isMore = module.queuedFileCount + module.uploadedCount >= module.maxFileCount;
        if (isMore) {
            this.trigger('error', 'Q_EXCEED_NUM_LIMIT', module.maxFileCount, file);
        }
        return !isMore;
    }
    //文件上传失败
    module.webUploaderError = function (file, errEvent) {
        $.removeupLoading();
        $.popup("上传失败");
        module.hasExcelFile = false;
        if (errEvent != undefined && errEvent != null) {
            errEvent();
        }
    }
    ///文件上传进度事件
    module.webUploaderProgress = function (file, percentage) {
        $.uploading();
        var num = (percentage * 100).toFixed(2);
        if (num >= 100) {
            $(".upload_div .upload_title").html("上传完成，正在处理文件，请等待...");
        }
        $(".up_percentNum").html(num + "%");
        $(".upload_percent").css("width", num + "%");
    }
    ///文件上传成功事件
    module.webUploaderSuccess = function (response, ft, successEvent, obj, errEvent) {
        if (response.res) {//上传成功！
            if (module.uploadedCount >= module.maxFileCount) {
                $.popup("最多只能上传" + module.maxFileCount + "个附件");
            }
            else {
                if (response.FileInfo == undefined) {
                    successEvent(response);
                    return;
                }
                if (ft == 0 && response["PrevPath"] == "") {
                    $.popup("转码失败！");
                    return false;
                }
                if (module.queuedFileCount > 0) {
                    module.queuedFileCount--;
                }
                module.uploadedCount++;
                var pickId = undefined;
                if (obj != undefined) {
                    pickId = obj.options.pick.id;
                }
                successEvent(response.FileInfo, pickId);
            }
        }
        else {
            $.popup(response.msg);
            if (errEvent != undefined && errEvent != null) {
                errEvent();
            }
        }
        $.removeupLoading();
    }
    ///文件上传完成事件
    module.webUploaderComplete = function (file, successEvent) {
        $.removeupLoading();
    }
    ///上传控件错误（1：图片，0：文档，2：视频，5：图片和文档）
    module.uploaderObjError = function (e, flType, exName, errEvent) {
        var errorMessage = "";
        if (e == "F_EXCEED_SIZE" || e == "Q_EXCEED_SIZE_LIMIT") {
            if (flType == 1) {
                errorMessage = "所选文件大小超过限制(10MB)";
            }
            else if (flType == 2) {
                errorMessage = "所选文件大小超过限制(200M)";
            }
            else if (flType == 0) {
                errorMessage = "所选文件大小超过限制(20MB)";
            }
            else if (flType == 5 || flType == -1) {
                errorMessage = "所选文件大小超过限制(10MB)";
            }
        }
        else if (e == "Q_TYPE_DENIED") {
            if (flType == 1) {
                errorMessage = "只能上传jpg,jpeg,png,gif,bmp格式的图片";
            }
            else if (flType == 2) {
                errorMessage = "只能上传Mp4格式的视频文件";
            }
            else if (flType == 0) {
                if (exName != undefined) {
                    errorMessage = "只能上传" + exName + "文件";
                }
                else {
                    errorMessage = "只能上传pdf,docx,doc,xls,xlsx,ppt,pptx格式的文件";
                }
            }
            else if (flType == 3) {
                errorMessage = "只能上传图片和pdf,docx,doc,xls,xlsx,ppt,pptx格式的文档";
            }
            else {
                errorMessage = "上传文件与设定的文件格式不符";
            }
        }
        else if (e == "Q_EXCEED_NUM_LIMIT") {
            errorMessage = "最多只能上传" + module.maxFileCount + "个附件";
        }
        if (errorMessage != "") {
            $.popup(errorMessage)
        }
        module.hasExcelFile = false;
        if (errEvent != undefined && errEvent != null) {
            errEvent();
        }
        $.removeupLoading();
    }

    ///添加参数
    module.AddUploderExcelPara = function (para) {
        module.uploaderExcel.on("uploadBeforeSend", function (obj, data, header) {
            for (var d in para) {
                data[d] = para[d];
            }
        });
    }
    ///导入Excel数据
    module.ImportExcelData = function () {
        module.uploaderExcel.upload();
    }
    //清空上传队列
    module.EmptyUploaderExcel = function () {
        module.hasExcelFile = false;
    }
    window.QEWebUploader = module;
    return module;
})(YzWebUploader || {
})






